/*
 * Copyright 2008 The Brui Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

package com.googlecode.brui.components.table;

import javax.swing.table.AbstractTableModel;

import com.googlecode.brui.beans.Bean;
import com.googlecode.brui.beans.Property;

/**
 * Table model for accessing a single bean as a list of name/value pairs. The
 * table will have two columns, one for property names, and one for property
 * values. Only the cells in the value column are editable. TODO collection
 * behaviour.
 */
public class BeanPropertyTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	/** The bean to create a table from. */
	private Object bean;

	/**
	 * Create an empty Table Model.
	 */
	public BeanPropertyTableModel() {
	}

	/**
	 * Create a Table Model for accessing the properties of the given bean.
	 * 
	 * @param bean the bean object to
	 */
	public BeanPropertyTableModel(final Object bean) {
		this.bean = bean;
	}

	/**
	 * @return the table model's bean.
	 */
	public Object getBean() {
		return bean;
	}

	/**
	 * Set the table model's bean.
	 * 
	 * @param bean the new bean to access from the table model.
	 */
	public void setBean(final Object bean) {
		this.bean = bean;
		fireTableDataChanged();
	}

	@Override
	public int getColumnCount() {
		return 2;
	}

	@Override
	public String getColumnName(final int columnIndex) {
		switch (columnIndex) {
			case 0:
				return "Property"; // TODO i18n
			case 1:
				return "Value";
			default:
				throw new IndexOutOfBoundsException("No such column index: '" + columnIndex + "'.");
		}
	}

	@Override
	public int getRowCount() {
		return getBeanWrapper() == null ? 0 : getBeanWrapper().getProperties().size();
	}

	@Override
	public Object getValueAt(final int rowIndex, final int columnIndex) {
		final Property property = getProperty(rowIndex);
		if (property == null) {
			return null;
		}
		switch (columnIndex) {
			case 0:
				return property.getDisplayName();
			case 1:
				return property.getValue();
			default:
				throw new IndexOutOfBoundsException("No such column index: '" + columnIndex + "'.");
		}
	}

	private Property getProperty(final int index) {
		return getBeanWrapper() == null ? null : getBeanWrapper().getProperties().get(index);
	}

	@Override
	public boolean isCellEditable(final int rowIndex, final int columnIndex) {
		return columnIndex == 1;
	}

	@Override
	public void setValueAt(final Object value, final int rowIndex, final int columnIndex) {
		// TODO check the value's type
		if (getBeanWrapper() != null) {
			getProperty(rowIndex).setValue(value);
		}
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	private Bean getBeanWrapper() {
		return bean == null ? null : new Bean(bean);
	}
}
